package com.github.cdisk.xtrans.core;

import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.github.cdisk.xtrans.annotiation.Attribute;
import com.github.cdisk.xtrans.annotiation.ConnectLine;
import com.github.cdisk.xtrans.annotiation.DefaultLine;
import com.github.cdisk.xtrans.connect.Connect;
import com.github.cdisk.xtrans.exception.BuildException;

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "_typeName")  
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = BaseComponent.class)
public abstract class BaseComponent {

	/** 执行状态_执行成功 */
	public static final int EXECUTE_SUCCESS = 1;

	/** 执行状态_执行失败 */
	public static final int EXECUTE_FAILED = 2;

	/** 执行状态_执行错误 */
	public static final int EXECUTE_ERROR = 3;

	protected GlobalVariables variables;
	
	/** 元件ID */
	@Attribute(value="名称", group="基础")
	protected String id;

	/** 元件启停状态 */
	@Attribute(value="启停状态", group="基础")
	protected boolean enable = true;

	/** 执行数据库目标 */
	@Attribute(value="数据库连接", group="基础")
	protected Connect connect;

	/** 元件执行成功后的执行过程 */
	@DefaultLine
	@ConnectLine(value="执行成功", color="99CC00", icon="tick.png")
	@Attribute(value="成功后执行过程", group="执行")
	protected BaseComponent success;

	/** 元件执行失败后的执行过程 */
	@ConnectLine(value="执行失败", color="FF9900", icon="error.png")
	@Attribute(value="失败后执行过程", group="执行")
	protected BaseComponent failed;

	/** 元件执行错误后的执行过程 */
	@ConnectLine(value="执行错误", color="CC0000", icon="cross.png")
	@Attribute(value="错误后执行过程", group="执行")
	protected BaseComponent error;

	@Attribute(value="UI", group="展示")
	protected BaseComponentUI ui;


	public BaseComponent() {
	}

	/**
	 * 元件的输入执行过程
	 * 
	 * @param input
	 *            输入参数
	 * @return 返回执行结果状态<br>
	 * {@link #EXECUTE_SUCCESS} 0x1<br>
	 * {@link #EXECUTE_FAILED} 0x2<br>
	 * {@link #EXECUTE_ERROR} 0x3 
	 */
	public abstract int execute(Map<String, Object> input);

	/**
	 * 检查元件各属性配对情况
	 * @param variables 
	 * @param connects 整个转换中的所有连接数组
	 * @param components 整个转换中的所有元件
	 * @throws ParserConfigurationException
	 */
	public void build(ConfigFile config, GlobalVariables variables) throws BuildException {
		this.variables = variables;
	}

	/**
	 * 获取是否连接了某个元件，用于读取配置时，找出根节点元件
	 * @return
	 */
	public boolean isConnectComponent(BaseComponent component) {
		if (success == component) return true;
		if (failed == component) return true;
		if (error == component) return true;
		return false;
	}

	/**
	 * 获取元件ID
	 * @return
	 */
	public String getId() {
		return id;
	}

	/**
	 * 设置元件ID
	 * @param id
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * 获取元件启停状态
	 * @return
	 */
	public boolean isEnable() {
		return enable;
	}

	/**
	 * 设置元件启停状态
	 * @param enable
	 */
	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	/**
	 * 获取元件执行的连接对象
	 * @return
	 */
	public Connect getConnect() {
		return connect;
	}

	/**
	 * 设置元件执行的连接对象
	 * @param connect
	 */
	public void setConnect(Connect connect) {
		this.connect = connect;
	}

	public BaseComponent getSuccess() {
		return success;
	}

	public void setSuccess(BaseComponent success) {
		this.success = success;
	}

	public BaseComponent getFailed() {
		return failed;
	}

	public void setFailed(BaseComponent failed) {
		this.failed = failed;
	}

	public BaseComponent getError() {
		return error;
	}

	public void setError(BaseComponent error) {
		this.error = error;
	}

	public BaseComponentUI getUi() {
		return ui;
	}

	public void setUi(BaseComponentUI ui) {
		this.ui = ui;
	}

}
